//
//  YFUserApi.m
//  CoollangTennisBall
//
//  Created by Coollang on 2018/1/13.
//  Copyright © 2018年 Coollang-YF. All rights reserved.
//

#import "YFUserApi.h"
#import "UIImage+HXExtension.h"


@implementation YFUserApi {
   
}

+ (instancetype)requestGetUserInfoWithComplate:(void(^)(BOOL success, UserInforItem *infoitem))complateBlock {
    YFUserApi *userInfoApi = [YFUserApi requestApiWithType:kUserGetInfoApi andParam:nil];
    [[YFNetworkingService sharedInstance] sendRequest:userInfoApi callback:^(BOOL success, id result, NSError *error) {
        if (success) {
            UserInforItem *infoItem = [[UserInforItem alloc] init];
            NSDictionary *dic = [result responseInfo];
            [infoItem parseUserInfoDictionary:dic];
            if (complateBlock) {
                complateBlock(YES,infoItem);
            }
        }else {
            if (complateBlock) {
                complateBlock(NO,nil);
            }
        }
    }];
    return userInfoApi;
}

/* --保存个人信息--
 UserName 不能为空
 Sex  不能为空
 Birthday  可为空,直接不传 或传空字符串
 Height 可为空,直接不传 或传空字符串
 Weight 可为空,直接不传 或传空字符串
 SportAge 可为空,直接不传 或传空字符串
 Hand 可为空,直接不传 或传空字符串
 */
+ (instancetype)requestSvaeUserInfoWithUserName:(NSString *)userName
                                       birthday:(NSString *)birthday
                                         isMale:(NSString *)gender
                                         height:(NSString *)height
                                         weight:(NSString *)weight
                                      signature:(NSString *)signature
                                       SportAge:(NSString *)sportAge
                                        Address:(NSString *)address
                                           Hand:(NSString *)hand
                                     RaketBrand:(NSString *)brand
                                StartTennisYear:(NSString *)startTennisYear
                                          Level:(NSString *)tennisLevel
                                       complete:(void(^)(NSError *error))completeBlock {
    
    NSDictionary *paramDict = @{@"UserName": [NSString checkIfNullWithString:userName],
                                @"Birthday":[NSString checkIfNullWithString:birthday],
                                @"Sex":[NSString checkIfNullWithString:gender],
                                @"Height":[NSString checkIfNullWithString:height],
                                @"Weight":[NSString checkIfNullWithString:weight],
                                @"Signature":[NSString checkIfNullWithString:signature],
                                @"SportAge":sportAge,
                                @"Address":[NSString checkIfNullWithString:address],
                                @"Hand":[NSString checkIfNullWithString:hand],
                                @"Brand":[NSString checkIfNullWithString:brand],
                                @"Type":@"1",
                                @"StartTennisYear":[NSString checkIfNullWithString:startTennisYear],
                                @"Level":[NSString checkIfNullWithString:tennisLevel],
                                };
    YFUserApi *saveUserApi = [YFUserApi requestApiWithType:kUserSaveInfoApi andParam:paramDict];
    [[YFNetworkingService sharedInstance] sendRequest:saveUserApi callback:^(BOOL success, id result, NSError *error) {
        if (completeBlock) {
            completeBlock(error);
        }
    }];
    return saveUserApi;
}
/**
 * 读取已得勋章
 */
+ (instancetype)initWithReadMedalsString:(NSString *)medalsString {
    return [YFUserApi requestApiWithType:kUserGetShowMedalApi andParam:@{@"medals":medalsString}];
}



/**
 * 1.获取个人主页统计数据
 * 2.获取个人主页年统计数据
 * 3.获取装备信息
 * 4.获取生涯列表
 */
+ (instancetype)requestGetUserDataWithReqType:(RequestApiType)apiType {
    return [YFUserApi requestApiWithType:apiType andParam:nil];
}
/**
 * 保存装备信息
 */
+ (instancetype)performSaveEquipInfoWithBrand:(NSString *)brand
                                      buyTime:(NSString *)buyTime
                                 verticalLine:(NSString *)verticalLine
                                         line:(NSString *)line
                                    shoesName:(NSString *)shoesName
                                 shoesBuyTime:(NSString *)shoesBuyTime {
    NSDictionary *dict = @{
                           @"brand":[NSString checkIfNullWithString:brand],
                           @"buyTime":[NSString checkIfNullWithString:buyTime],
                           @"verticalLine":[NSString checkIfNullWithString:verticalLine],
                           @"line":[NSString checkIfNullWithString:line],
                           @"shoesName":[NSString checkIfNullWithString:shoesName],
                           @"buyTime":[NSString checkIfNullWithString:buyTime],
                           @"shoesBuyTime":[NSString checkIfNullWithString:shoesBuyTime]
                           };
    return [YFUserApi requestApiWithType:KUserSaveEquipApi andParam:dict];
}



/**
 * 删除生涯
 */
+ (instancetype)deleteCareerWithCareerID:(NSString *)careerId complete:(void(^)(NSError *error))complateBlock {
    YFUserApi *deleteCareerApi = [YFUserApi requestApiWithType:kUserDeleteCareerDataApi andParam:@{@"careerID":[NSString checkIfNullWithString:careerId]}];
    [[YFNetworkingService sharedInstance] sendRequest:deleteCareerApi callback:^(BOOL success, id result, NSError *error) {
        if (complateBlock) {
            complateBlock(error);
        }
    }];
    return deleteCareerApi;
}

/**
 * 添加或修改生涯
 * careerId: 值为nil时为添加生涯
 * year:  生涯的时间，单位（年）
 * careerContent:生涯事迹内容
 */
+ (instancetype)addOrChangeCareerWithCareerID:(NSString *)careerId year:(NSString *)year careerContent:(NSString *)careerContent {
    NSDictionary *dict;
    if (careerId.length > 0) {
        dict = @{@"year":year, @"career":[NSString checkIfNullWithString:careerContent], @"ID":careerId};
    }else {
        dict = @{@"year":year, @"career":[NSString checkIfNullWithString:careerContent]};
    }
    return [YFUserApi requestApiWithType:kUserAddCareerDataApi andParam:dict];
}

/**
 * 上传头像，鞋子，装备图片
 */
+ (instancetype)uploadImageWithRequestType:(RequestApiType)apiType withImage:(UIImage *)image complete:(void(^)(NSError *error,NSString *imageUrl))complateBlock {
    if (image == nil) {
        if (complateBlock) {
            complateBlock([NSError errorWithDomain:@"Pparameter error" code:WebRespondTypeArgumentError],nil);
        }
        return nil;
    }
    YFUserApi *uploadImageApi = [YFUserApi requestApiWithType:apiType andParam:nil];
    
    uploadImageApi.constructingBodyBlock = ^(id<AFMultipartFormData>  _Nonnull formData) {
        NSData *data = [image compressToDataHX];
        NSString *name = @"image";
        NSString *formKey = @"userfile";
        NSString *type = @"png";
        [formData appendPartWithFileData:data name:formKey fileName:name mimeType:type];
    };
    
    [[YFNetworkingService sharedInstance] sendRequest:uploadImageApi callback:^(BOOL success, id result, NSError *error) {
        if (complateBlock) {
            complateBlock(error,[result responseInfo]);
        }
    }];
    return uploadImageApi;
}

/*
- (AFConstructingBlock)constructingBodyBlock {
    if  (self.apiType == kUserUploadHeadImage || self.apiType == KUserUploadShoeImageApi || self.apiType == KUserUploadRactetImageApi) {
        return ^(id<AFMultipartFormData> formData) {
            NSData *data = [self.uploadImage compressToDataHX];
            NSString *name = @"image";
            NSString *formKey = @"userfile";
            NSString *type = @"png";
            [formData appendPartWithFileData:data name:formKey fileName:name mimeType:type];
        };
    }else if (self.apiType == kUserFeedbackApi){
        return ^(id<AFMultipartFormData> formData) {
            for (NSInteger i = 0; i< self.feedbackImages.count; i++) {
                UIImage *image = self.feedbackImages[i];
                NSData *data = [image compressToDataHX];
                NSString *name = [NSString stringWithFormat:@"feedBackImage_%zd_userID:%@.jpg",i,[User getLastUser].userInforItem.userID];
                NSString *formKey =[NSString stringWithFormat:@"userfile%zd",i+1];
                NSString *type = @"multipart/form-data";//@"image/jpg";
                
                [formData appendPartWithFileData:data name:formKey fileName:name mimeType:type];
            }
        };
    }else {
        return [super constructingBodyBlock];
    }
}
*/


/**
 * 用户反馈-图片大小限制是1024k
 * email 不能为空， content和images至少得有一个
 */
+ (instancetype)feedbackWithImages:(NSArray <UIImage *>*)images email:(NSString *)email content:(NSString *)content complete:(void(^)(NSError *error))complateBlock {
    
    User *user = [User getLastUser];
    content = content.length <= 0 ? @"系统提示:该用户没有上传文字过来!":content;
    NSString *userEmial = [NSString checkIfNullWithString:user.userInforItem.email];
    if (userEmial.length <= 0) {
        userEmial = @"KL@coollang.com";
    }
    email = email.length <= 0 ?userEmial:email;
    NSString *firmware = user.userDevice.locationVersion;
    
    NSDictionary *paramDict = @{@"email":email,
                                @"content":content,
                                @"firmware":firmware,
                                @"softVersion":[NSString appCurrentVersion],
                                };
    YFUserApi *feedbackApi = [YFUserApi requestApiWithType:kUserFeedbackApi andParam:paramDict];
    
    feedbackApi.constructingBodyBlock = ^(id<AFMultipartFormData>  _Nonnull formData) {
        for (NSInteger i = 0; i< images.count; i++) {
            UIImage *image = images[i];
            NSData *data = [image compressToDataHX];
            NSString *name = [NSString stringWithFormat:@"feedBackImage_%zd_userID:%@.jpg",i,[User getLastUser].userInforItem.userID];
            NSString *formKey =[NSString stringWithFormat:@"userfile%zd",i+1];
            NSString *type = @"multipart/form-data";//@"image/jpg";
            [formData appendPartWithFileData:data name:formKey fileName:name mimeType:type];
        }
    };
    
    [[YFNetworkingService sharedInstance] sendRequest:feedbackApi callback:^(BOOL success, id result, NSError *error) {
        if (complateBlock) {
            complateBlock(error);
        }
    }];
    return feedbackApi;
}


@end
